package com.pro.service.imple;

import com.pro.mapper.DepartmentMapper;
import com.pro.pojo.*;
import com.pro.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：刘弘禹
 * @description：TODO
 * @date ：2022/1/11 8:51
 */
@Service
public class DepartmentServiceImple implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 查询部门列表
     *@param jsDept
     *  部门信息，根据信息筛选对应数据返回
     *@return 返回部门列表集合
     * @author 刘弘禹
     * @Time 2022-1-11 15:53
     */
    @Override
    public List<JsDept> queryDept(JsDept jsDept) {
        List<JsDept> jsDepts = departmentMapper.queryDept(jsDept);
        return jsDepts;
    }

    /**
     * 修改部门状态
     *@param jsDept
     *  跟具id修改部门状态
     *@return 返回状态修改是否成功
     * @author 刘弘禹
     * @Time 2022-1-12 16:02:20
     */
    @Override
    public Boolean updateDeptStatus(JsDept jsDept) {
        Boolean bol = departmentMapper.updateDeptStatus(jsDept);
        return bol;
    }

    /**
     * 修改部门名称和信息
     *@param jsDept
     *  跟具id修改部门信息
     *@return 返回状态修改是否成功
     * @author 刘弘禹
     * @Time 2022-1-12 18:44:05
     */
    @Override
    public Boolean updateDept(JsDept jsDept) {
        Boolean bol = departmentMapper.updateDept(jsDept);
        return bol;
    }
    /**
     * 添加部门
     *@param jsDept
     *  添加一个部门
     *@return 返回状态修改是否成功
     * @author 刘弘禹
     * @Time 2022-1-12 20:31:33
     */
    @Override
    public Boolean addDept(JsDept jsDept) {
        Boolean bol = departmentMapper.addDept(jsDept);
        return bol;
    }


    /**
     * 查询后台人员
     *@param jsUser
     *  查询所有后台人员
     *@return 返回所有后台人员列表
     * @author 刘弘禹
     * @Time 2022-1-13 19:46:05
     */
    @Override
    public List<JsUser> getJsUser(JsUser jsUser) {
        List<JsUser> jsUsers = departmentMapper.getJsUser(jsUser);
        return jsUsers;
    }

    /**
     * 查询一个后台人员详细信息
     *@param id
     *  根据id查询一个后台人员详细信息
     *@return 返回一个后台人员信息
     * @author 刘弘禹
     * @Time 2022-1-14 15:48:33
     */
    @Override
    public JsUser getJsUserOn(Integer id) {
        JsUser jsUserOn = departmentMapper.getJsUserOn(id);
        return jsUserOn;
    }

    /**
     * 查询后台人员权限信息
     *@param id
     *  根据id查询一个后台人员权限信息
     *@return 返回一个后台人员权限信息
     * @author 刘弘禹
     * @Time 2022-1-15 13:42:12
     */
    @Override
    public List<Integer> getPermission(Integer id) {
        List<Integer> permission = departmentMapper.getPermission(id);
        return permission;
    }


    /**
     * 删除后台人员权限
     *@param userId,deptId
     *  删除权限信息
     *@return 返回是否成功
     * @author 刘弘禹
     * @Time 2022-1-15 15:28:12
     */
    @Override
    public Boolean deleteJurisdiction(Integer userId, Integer deptId) {
        Boolean bol = departmentMapper.deleteJurisdiction(userId, deptId);
        return bol;
    }

    /**
     * 添加后台人员权限
     *@param userId,deptId
     *  添加权限信息
     *@return 返回是否成功
     * @author 刘弘禹
     * @Time 2022-1-15 15:35:12
     */
    @Override
    public Boolean addJurisdiction(Integer userId, Integer deptId) {
        Boolean bol = departmentMapper.addJurisdiction(userId, deptId);
        return bol;
    }

    /**
     * 添加后台用户
     *@param jsUser
     *  添加后台用户
     *@return 返回是否成功
     * @author 刘弘禹
     * @Time 2022-1-18 14:22:10
     */
    @Override
    public Boolean addUser(JsUser jsUser) {
        Boolean bol = departmentMapper.addUser(jsUser);
        return bol;
    }

    /**
     * 获取部门信息
     *@return 返回部门信息
     * @author 刘弘禹
     * @Time 2022-1-18 20:10:12
     */
    @Override
    public List<JsDept> getZtree() {
        List<JsDept> ztree = departmentMapper.getZtree();
        List<JsDept> newZtree = new ArrayList<>();
        System.err.println(ztree);
        for (int i = 0; i<ztree.size();i++ ){
            if(ztree.get(i).getParentId() == 0){
                newZtree.add(ztree.get(i));
            }
            for (int k = 0; k<ztree.size();k++ ){
                if(ztree.get(i).getId() == ztree.get(k).getParentId()){
                    ztree.get(i).getChildren().add(ztree.get(k));
                }
            }
        }
        return newZtree;
    }

    /**
     * 获取后台用户信息
     *@param userId
     *  获取后台用户信息
     *@return 返回后台用户
     * @author 刘弘禹
     * @Time 2022-1-20 21:08:10
     */
    @Override
    public JsUser getUserById(Integer userId) {
        JsUser userById = departmentMapper.getUserById(userId);
        return userById;
    }

    /**
     * 修改后台用户信息
     *@param jsUser
     *  修改后台用户信息
     *@return 返回是否成功
     * @author 刘弘禹
     * @Time 2022-1-20 21:30:22
     */
    @Override
    public Boolean editUserById(JsUser jsUser) {
        System.err.println(jsUser);
        Boolean bol = departmentMapper.editUserById(jsUser);
        return bol;
    }

    /**
     * 查询封停日志
     *@return 返回封停日志信息
     * @author 刘弘禹
     * @Time 2022-1-21 20:35:12
     */
    @Override
    public List<Unseal> getUnsealList() {
        List<Unseal> unsealList = departmentMapper.getUnsealList();
        return unsealList;
    }

    /**
     * 查询封停日志
     *@return 返回封停日志信息
     * @author 刘弘禹
     * @Time 2022-2-8 14:03:22
     */
    @Override
    public Boolean updateEnable(Integer id, Integer enabled) {
        Boolean bol = departmentMapper.updateEnable(id, enabled);
        return bol;
    }

    /**
     * 删除后台部门
     *@return 删除后台部门
     * @author 刘弘禹
     * @Time 2022-2-9 11:28:22
     */
    @Override
    public Boolean removeDept(Integer id) {
        Boolean bol = departmentMapper.removeDept(id);
        return bol;
    }

    /**
     * 账号封停
     *@return 账号封停
     * @author 刘弘禹
     * @Time 2022-2-9 14:48:22
     */
    @Override
    @Transactional
    public Boolean updateUserType(JsClosure jsClosure) {
        Boolean bol = departmentMapper.updateUserType(jsClosure);
        if(bol){
            Boolean addcloBol = departmentMapper.addClosure(jsClosure);
            return addcloBol;
        }
        return false;
    }


}
